﻿using System;
using System.Data;
using System.Windows.Forms;
using HM.TESTCS;
using System.Drawing;

namespace HM
{
    public partial class StockQuery : Form
    {
        public StockQuery()
        {
            InitializeComponent();
            SetFormWithAndHeith(this);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void StockQuery_Closed(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void txtBarCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                if (!string.IsNullOrEmpty(txtBarCode.Text.Trim()))
                {
                    if (txtBarCode.Text.Trim() == "0920069990663")
                    {
                        lvStockQuery.Items.Clear();
                        FillListView(new ApploginTest().GetItemStockInfo(txtBarCode.Text.Trim()));
                    }
                    else if (txtBarCode.Text.Trim() == "0930699991565")
                    {
                        lvStockQuery.Items.Clear();
                        FillListView(new ApploginTest().GetItemStockInfo1(txtBarCode.Text.Trim()));
                    }
                    else
                    {
                        lvStockQuery.Clear();
                        MessageBox.Show("输入的条码信息商品不存在,请确认");
                        txtBarCode.Focus();
                    }
                }
                else
                {
                    MessageBox.Show("请输入条码信息");
                }
            }
        }


        public void FillListView(DataTable dtSrcDetail)
        {
            try
            {
                if (dtSrcDetail.Rows.Count > 0)
                {

                    foreach (DataRow dr in dtSrcDetail.Rows)
                    {
                        string itemName;
                        string unitName;
                        string stockName;
                        string stockSiteName;
                        string stockQty;
                        string batch;

                        itemName = dr["itemName"].ToString();
                        unitName = dr["unitName"].ToString();
                        stockName = dr["stockName"].ToString();
                        stockSiteName = dr["stockSiteName"].ToString();
                        stockQty = dr["stockQty"].ToString();
                        batch = dr["batch"].ToString();

                        ListViewItem lvi = new ListViewItem();
                        //首列赋值
                        lvi.Text = itemName.ToString();
                        lvi.SubItems.Add(unitName);
                        lvi.SubItems.Add(stockName);
                        lvi.SubItems.Add(stockSiteName);
                        lvi.SubItems.Add(stockQty);
                        lvi.SubItems.Add(batch);


                        lvStockQuery.Items.Add(lvi);
                    }
                    //结束数据处理，UI界面一次性绘制。
                    lvStockQuery.EndUpdate();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("抱歉,错误的原因是:" + e.ToString());
            }
        }


        private void SetFormWithAndHeith(Form obj)
        {
            Rectangle ScreenArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
            int width = ScreenArea.Width;
            int height = ScreenArea.Height;
            obj.Width = width - 5;
            obj.Height = height - 5;
        }
    }
}